# 导入图片资源
from imageResource import imageResource


# 编写飞机类
class Plane:
    def __init__(self, x, y):
        # 飞机的大小
        self.size = 200
        # 设置飞机的飞行速度
        self.speed = 5
        # 飞机的位置
        self.x = x
        self.y = y
        # tkinter 读取图片文件
        self.img = imageResource.getPlaneImg()
        # 飞机的id
        self.id = None
        # 当前飞机是否死亡的标志 - 死亡 True - 未死亡 False
        self.isDie = False

    def move(self, keys=None):
        # elif 全部 改成 if 就可以让飞机斜着飞了
        if keys['a']:
            self.x -= self.speed
        if keys['d']:
            self.x += self.speed
        if keys['w']:
            self.y -= self.speed
        if keys['s']:
            self.y += self.speed

    # 显示的方法
    def show(self, canvas):
        canvas.show(self)

    # 清理飞机的逻辑
    def isClear(self, winWidth, winHeight):
        return self.isDie

    # 是否产生碰撞的逻辑 - 如果碰撞 返回True 如果不碰撞 返回False
    def isCollision(self, obj):
        # Ax = self.x - self.size // 2
        # Ay = self.y - self.size // 2
        # Asize = self.size
        # Bx = obj.x - obj.size // 2
        # By = obj.y - obj.size // 2
        # Bsize = obj.size
        Asize = self.size // 2
        Bsize = obj.size // 2
        Ax = self.x - Asize // 2
        Ay = self.y - Asize // 2
        Bx = obj.x - Bsize // 2
        By = obj.y - Bsize // 2

        t1 = Ax + Asize < Bx
        t2 = Ax > Bx + Bsize
        t3 = Ay + Asize < By
        t4 = Ay > By + Bsize

        return not (t1 or t2 or t3 or t4)

class PlaneFactory:
    def __init__(self):
        self.list = []

    # 创建飞机
    def create(self, x, y):
        obj = Plane(x, y)
        self.list.append(obj)
        return obj
    
    # 自己的飞机就不需要清理了
    def flush(self, winWidth, winHeight):
        pass

planeFactory = PlaneFactory()